<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['delete', 'quickSearch']"
            :quick-search-placeholder="t('客户名/主播名')"
        >
            <template #btnBefore>
                <span style="" class="btn btn-success ml-12" @click="router.push({ path: 'live/add' })">创建直播</span>
            </template>
        </TableHeader>


        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #out_trade_no>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column prop="id" label="直播编号" align="center" width="260">
                    <template #default="scope">
                        <div class="live-number" @click="detail(scope.row.out_trade_no)">
                            {{ scope.row.out_trade_no }}
                        </div>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <!-- 直播记录详情 -->
        <Detail 
            v-if="state.live_id > 0" 
            :live_id="state.live_id" 
            @hideDetail="hideDetail"
        />

    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Detail from './detail.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import router from '/@/router'

defineOptions({
    name: 'live',
})

const { t } = useI18n()

const state = reactive({
    live_id: 0,

})

/**
 * 关闭订单详情
 */
const hideDetail = () => {
    state.live_id = 0;
}

/**
 * 打开订单详情
 */
const detail = (live_id: number) => {
    window.open('/user/live/detail/' + live_id)
}








const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/user/order.Order/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { 
                label: t('直播编号'), 
                prop: 'out_trade_no', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE',
                render: 'slot',
                slotName: 'out_trade_no'
            },
            { 
                label: t('live.live_time'), 
                prop: 'live_time', 
                align: 'center', 
                operator: 'RANGE', 
                sortable: false
            },
            { label: t('主播'), prop: 'anchor_name', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('live.create_time'), prop: 'create_time', align: 'center', operator: 'RANGE', sortable: 'custom', width: 160, timeFormat: 'yyyy-mm-dd hh:MM:ss' },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { user_id: null, brand_id: null, goods_ids: null, anchor: null, live_time: 0 },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.table.filter!.type = 0;
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">

.live-number{
    cursor: pointer;
}
.live-number:hover{
    text-decoration: underline;
}

:deep(.ba-operate-dialog .el-dialog__body){
    padding-bottom: 20px;
    padding-left: 0;
    padding-right: 0;
}
:deep(.el-image){
    width: 60px;
    height: unset;
}



</style>
